class_name BehaviorTreeSelector
extends BehaviorTreeControl

# 此节点顺序执行子节点，当其中一个子节点返回成功时该节点返回成功，若全部子节点返回失败时该节点返回失败


func _on_enter():
	_current_index = 0

	if _current_index < _children.size():
		var current: BehaviorTreeNode = _children[_current_index]
		if current:
			current._on_enter()



func _tick(delta: float) -> NodeState:
	if _current_index >= _children.size():
		return NodeState.FAILURE

	var current: BehaviorTreeNode = _children[_current_index]
	if not current:
		return NodeState.FAILURE

	var state := current._tick(delta)

	if state == NodeState.RUNNING:
		return NodeState.RUNNING
	elif state == NodeState.SUCCESS:
		current._on_exit()
		return NodeState.SUCCESS
	elif state == NodeState.FAILURE:
		current._on_exit()

	_current_index += 1

	if _current_index < _children.size():
		var next: BehaviorTreeNode = _children[_current_index]
		if next:
			next._on_enter()

	return _tick(delta)